package com.maoning.auth.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.maoning.auth.feign.MemberFeignService;
import com.maoning.common.constant.AuthServerConstant;
import com.maoning.common.utils.HttpUtils;
import com.maoning.common.utils.R;
import com.maoning.common.vo.MemberRespVo;
import com.maoning.common.vo.SocialUser;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : OAuth2Controller
 * @Author : Administrator
 * @Date: 2022/4/6 12:59
 */
@Controller
@RequestMapping("/oauth2.0")
public class OAuth2Controller {
    @Autowired
    private MemberFeignService memberFeignService;

    @GetMapping("/weibo/success")
    public String weiboSuccess(@RequestParam("code")String code, HttpSession session, RedirectAttributes redirectAttributes) throws Exception {
        //登陆成功,响应拿到code码,用来换取accesstoken,然后返回首页
        String host = "https://api.weibo.com";
        String path = "/oauth2/access_token";
        String method = "post";
        Map<String, String> querys = new HashMap<>();
        querys.put("client_id", "1931651844");
        querys.put("client_secret", "d8b29904b3fc303fdc4476649451996e");
        querys.put("grant_type", "authorization_code");
        querys.put("redirect_uri", "http://auth.gulimall.com/oauth2.0/weibo/success");
        querys.put("code", code);
        //成功以后,这个resp里就可以拿到access_token
        final HttpResponse resp = HttpUtils.doPost(host, path, method, null, querys, (Map<String, String>) null);
        if(resp.getStatusLine().getStatusCode() == 200){
            //获取成功
            final HttpEntity entity = resp.getEntity();
            final String json = EntityUtils.toString(entity);
            final SocialUser socialUser = JSONObject.parseObject(json, SocialUser.class);
            //判断是否是首次登陆,首次则注册一个,并关联
            //如果注册了就查出用户信息
            final R r = memberFeignService.oauth2Login(socialUser);
            if(r.getCode() == 0){
                final MemberRespVo data = r.getData(new TypeReference<MemberRespVo>() {
                });
                session.setAttribute(AuthServerConstant.LOGIN_USER, data);
                return "redirect:http://gulimall.com";
            }else {
                Map<String,String> errors = new HashMap<>();
                errors.put("msg", (String) r.get("msg"));
                redirectAttributes.addFlashAttribute("errors",errors);
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }else {
            //获取失败
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}
